package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.TestOptionsDao;
import com.woniuxy.dao.TestTestLabelDao;
import com.woniuxy.entity.*;
import com.woniuxy.dao.TestDao;
import com.woniuxy.exception.UnauthorizedException;
import com.woniuxy.service.TestService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Test)表服务实现类
 *
 * @author zjx
 * @since 2021-05-14 19:41:33
 */
@Service("testService")
public class TestServiceImpl implements TestService {
    @Resource
    private TestDao testDao;
    @Resource
    private TestTestLabelDao testTestLabelDao;
    @Resource
    private TestOptionsDao testOptionsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Test queryById(Integer id) {
        return this.testDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Test> queryAllByLimit(int offset, int limit) {
        return this.testDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param test 实例对象
     * @return 实例对象
     */
    @Override
    public Test insert(Test test) {
        test.setCreateTime(new Date());
        this.testDao.insert(test);
        return test;
    }

    /**
     * 修改数据
     *
     * @param test 实例对象
     * @return 实例对象
     */
    @Override
    public Test update(Test test) {
        test.setUpdateTime(new Date());
        this.testDao.update(test);
        return this.queryById(test.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.testDao.deleteById(id) > 0;
    }


    /**
     * Description : 利用事务，多表插入，实现试题创建
     * ChangeLog : 1. 创建 (2021/5/17 11:37 [zjx]);
     * @param map  一个map数据
     * @return null
    */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int insertOne(Map<String, Object> map) {
        //创建test对象
        Object o = map.get("Test");
        JSONObject jsonObject = JSONObject.fromObject(o);
        Test test = (Test) JSONObject.toBean(jsonObject, Test.class);
        test.setCreateTime(new Date());
        test.setUpdateTime(new Date());
        //插入test
        this.testDao.insert(test);
        //查询试题id
        Integer testId = test.getId();
        //获取所有标签id
        JSONArray jsonTL = JSONArray.fromObject(map.get("TestLabels"));
        ArrayList<TestLabel> testLabels = (ArrayList<TestLabel>) JSONArray.toCollection(jsonTL, TestLabel.class);
        //放入试题-试题标签中间表集合
        List<TestTestLabel> testTestLabels = new ArrayList<>();
        for (TestLabel testLabel:
                testLabels ) {
            testTestLabels.add(new TestTestLabel(testId,testLabel.getId(),test.getCreater(),new Date(),new Date()));
        }
        //插入试题-试题标签中间表
        this.testTestLabelDao.insertBatch(testTestLabels);
        //获取选项表
        JSONArray jsonOptions = JSONArray.fromObject(map.get("TestOptions"));
        ArrayList<TestOptions> lists = (ArrayList<TestOptions>) JSONArray.toCollection(jsonOptions, TestOptions.class);
        //设置testid
        for (TestOptions t:
             lists) {
            t.setTestId(testId);
            t.setCreateTime(new Date());
            t.setUpdateTime(new Date());
        }
        //插入选项表
        this.testOptionsDao.insertBatch(lists);
        return 1;
    }

    @Override
    public PageInfo<Map<String, String>> queryAll(Map<String, Object> map) {
        Map<String,String> testInfo = (Map<String, String>) map.get("testInfo");
        int startPage = (int) map.get("startPage");
        int pageSize = (int) map.get("pageSize");
        PageHelper.startPage(startPage,pageSize);
        System.out.println("testInfo====传入sql的值"+testInfo);
        Page<Map<String, String>> page = (Page<Map<String, String>>) this.testDao.queryAlls(testInfo);
        PageInfo<Map<String,String>> tests = new PageInfo<>(page);
        return tests;
    }

    @Override
    public Map<String, Object> queryByTestBankId(Map<String, Object> map) {
        Map<String, Object> info = this.testDao.queryByTestBankId(map);
        //参数校验
        if (info == null){
            return  null;
        }
        //获取试题内容
        Object o = info.get("testBody");
        //以每道题id和题干构成数组
        String[] ss = o.toString().split("@");
        //创建一个list
        List<Map<String,String>> list = new ArrayList<>();
        for (String s:
                ss) {
            //切割一道题的id 和 内容
            String[] split = s.split("::");
            //放入一个map中
            Map<String,String> testBodyAndId = new HashMap<>();
            testBodyAndId.put("testId",split[0]);
            testBodyAndId.put("testBody",split[1]);
            //存入集合中
            list.add(testBodyAndId);
        }
        info.put("list",list);
//        System.out.println("info==="+info);
        return info;
    }
}
